package com.hymjweb.demo.hymj.basic_data_manage.xt_city.mapper;

import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.bean.XtCityCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.bean.XtCityPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtCityMapper {


    class XtCityProvider {

        public String findAll(Map map) {

            String sql = new SQL() {{
                SELECT("t.id, t.code, t.name, t.pid, t.name_en, t.short_name_en,t.status");
                FROM("xt_city t where 1 = 1 ");
            }}.toString();

            if (map != null) {
                String keyname = DataTypeUtil.toNotNullString(map.get("keyname"));
                long status = DataTypeUtil.toLong(map.get("status"), -1);
                if (!DataTypeUtil.isEmptyStr(keyname)) {
                    sql += " and ( t.code like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.name like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.name_en like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.short_name_en like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += ")";
                }

                if (status != -1) {
                    sql += " and xa.status = " + status;
                }
            }
            return sql;
        }

        public String findCacheAll(Map map) {
            return new SQL() {{
                SELECT("t.id, t.code, t.name, t.pid, t.name_en, t.short_name_en");
                FROM("xt_city t");
                WHERE("t.status = " + SysConstant.XT_DIC_STATUS.VALID.getId());
            }}.toString();
        }

        public String fetch(String cityId) {
            return new SQL() {{
                SELECT("t.id, t.code, t.name, t.pid, t.name_en, t.short_name_en");
                FROM("xt_city t");
                WHERE("t.id = #{cityId}");
            }}.toString();
        }

        public String insert(XtCityPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("xt_city");
                VALUES("id", "#{id}");
                VALUES("pid", "#{pid}");
                VALUES("code", "#{code}");
                VALUES("name", "#{name}");
                VALUES("name_en", "#{nameEn}");
                VALUES("short_name_en", "#{shortNameEn}");
                VALUES("status", "#{status}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(XtCityPojo pojo) {
            String sql = new SQL() {{
                UPDATE("xt_city t");
                SET("t.pid = #{pid}");
                SET("t.code = #{code}");
                SET("t.name = #{name}");
                SET("t.name_en = #{nameEn}");
                SET("t.short_name_en = #{shortNameEn}");
                SET("t.status = #{status}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_city");
                WHERE("id = " + id);
            }}.toString();
            return sql;
        }
    }


    @Results(id = "cityMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "code", property = "code"),
            @Result(column = "name", property = "name"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "name_en", property = "nameEn"),
            @Result(column = "short_name_en", property = "shortNameEn"),
            @Result(column = "status", property = "status")
    })
    @SelectProvider(type = XtCityProvider.class, method = "findAll")
    List<XtCityPojo> findAll(Map map);


    @Results(id = "cityCacheMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "code", property = "code"),
            @Result(column = "name", property = "name"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "name_en", property = "nameEn"),
            @Result(column = "short_name_en", property = "shortNameEn")
    })
    @SelectProvider(type = XtCityProvider.class, method = "findCacheAll")
    List<XtCityCachePojo> findCacheAll(Map map);


    @ResultMap(value = "cityMapId")
    @SelectProvider(type = XtCityProvider.class, method = "fetch")
    XtCityPojo fetch(String cityId);

    @InsertProvider(type = XtCityProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtCityPojo obj);

    @UpdateProvider(type = XtCityProvider.class, method = "update")
    int update(XtCityPojo obj);

    @DeleteProvider(type = XtCityProvider.class, method = "delete")
    int delete(String id);


}
